package com.sgcc.pda.data.dao.impl;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;

/**
 * 消息服务Provider
 *  
 *
 * @author ShiPengHao
 * @date 2018/4/11
 */
public class MessageProviderDaoImpl {


    String CONTENT_PROVIDER_CONNECT = "";

    private static MessageProviderDaoImpl contentProviderDao;

    public static MessageProviderDaoImpl getInstance() {
        if (contentProviderDao == null) {
            contentProviderDao = new MessageProviderDaoImpl();
        }
        return contentProviderDao;
    }

    public void init(String contentProviderConnect) {
        CONTENT_PROVIDER_CONNECT = contentProviderConnect;
    }

    //更新消息状态
    private static final String UPDATE_MSG_STATE = "UPDATE_MSG_STATE";

    private static final String QUERY_MSG_BADGE = "QUERY_MSG_BADGE";// 查询未读消息

    /**
     * 更新工单消息状态
     *
     * @param context
     * @param state
     * @param orderId
     * @return
     */
    public boolean updateMsgState(Context context, int state, String orderId) {
        ContentResolver resolver = context.getContentResolver();
        //定义要访问的ContentProvider的
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + UPDATE_MSG_STATE);
        ContentValues v = new ContentValues();
        v.put("test", "test");
        int result = resolver.update(uri, v, null, new String[]{state + "", orderId});
        if (result == 1) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询未读消息个数
     *
     * @param context ctx
     * @return count
     */
    public int queryBadgeCount(Context context) {
        ContentResolver resolver = context.getContentResolver();
        // 定义要访问的ContentProvider的Uri
        Uri uri = Uri.parse(CONTENT_PROVIDER_CONNECT + QUERY_MSG_BADGE);
        Cursor cursor = resolver.query(uri, null, null, null, null);
        int count = 0;
        if (cursor != null) {
            count = cursor.getCount();
            cursor.close();
        }
        return count;
    }
}
